package dk.au.cs.bdsi.sms.activities;

import dk.au.cs.bdsi.core.activities.DeleteException;
import dk.au.cs.bdsi.core.representation.Identifier;
import dk.au.cs.bdsi.core.representation.ServiceUri;
import dk.au.cs.bdsi.sms.domain.Sms;
import dk.au.cs.bdsi.sms.domain.SmsStatus;
import dk.au.cs.bdsi.sms.repositories.SmsRepository;
import dk.au.cs.bdsi.sms.representation.SmsRepresentation;

public class DeleteSmsActivity {
	// TODO Make concurrent
	public SmsRepresentation delete(ServiceUri requestUri) {
		// Retrieve sms
		Identifier id = requestUri.getId();
		Sms sms = SmsRepository.current().get(id);
		
		// Ensure it exists and can be deleted
		if (sms == null) throw new NoSuchSmsException();
		SmsStatus status = sms.getStatus();
		if (status != SmsStatus.Sent && status != SmsStatus.NotSent) 
			throw new DeleteException();
		
		// Okay to delete
		SmsRepository.current().remove(id);
		return new SmsRepresentation(sms);
	}
}
